package com.ttc.group2.videochat.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.activity.ChatActivity;
import com.ttc.group2.videochat.adapter.PeopleListAdapter;
import com.ttc.group2.videochat.database.DbHelper;
import com.ttc.group2.videochat.model.Section;
import com.ttc.group2.videochat.model.User;
import com.ttc.group2.videochat.utils.Const;

public class PeopleFragment extends ListFragment {
	private static final String TAG = "PeopleFragment";
	private List<User> peoples;
	private PeopleListAdapter mAdapter = new PeopleListAdapter();
	private HashMap<String, Integer> sections = new HashMap<String, Integer>();

	public PeopleFragment() {
	};

	public static PeopleFragment newIntance() {
		PeopleFragment peoleFragment = new PeopleFragment();
		return peoleFragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		View view = inflater
				.inflate(R.layout.fragment_people, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "onActivityCreated");

		// get from server
		peoples = DbHelper.getInstance(getActivity()).getListUsers();

		List<User> rows = new ArrayList<User>();
		int start = 0;
		String previousLetter = null;
		Pattern numberPattern = Pattern.compile("[0-9]");

		for (User lUser : peoples) {
			String firstLetter = lUser.getFullName().substring(0, 1)
					.toUpperCase(Locale.getDefault());

			// Group numbers together
			if (numberPattern.matcher(firstLetter).matches()) {
				firstLetter = "#";
			}

			if (!firstLetter.equalsIgnoreCase(previousLetter)) {
				rows.add(new Section(firstLetter));
				sections.put(firstLetter, start);
			}
			User user = new User();
			user.setUserId(lUser.getUserId());
			user.setFullName(lUser.getFullName());
			user.setEmail(lUser.getEmail());
			rows.add(user);
			previousLetter = firstLetter;

		}

		mAdapter.setRows(rows);
		setListAdapter(mAdapter);

		// Set empty view
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if (l.getItemAtPosition(position) instanceof Section) {
			return;
		}
		User user = (User) l.getItemAtPosition(position);
		Intent intent = new Intent(getActivity(), ChatActivity.class);
		// intent = IntentUtils.putData(intent, user); // putExtra UserId
		intent.putExtra(Const.USER_ID, user.getUserId());
		intent.putExtra(Const.USER_NAME, user.getFullName());
		getActivity().startActivity(intent);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onResume");
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onPause");
		super.onPause();
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onStop");
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onDestroyView");
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.d(TAG, "onDestroy");
		super.onDestroy();
	}

}
